package cn.com.pxto.web.tag;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

import cn.com.pxto.dao.LessonTypeDAO;
import cn.com.pxto.service.SchoolManager;

public class SchoolLessonTypeTag extends BodyTagSupport {
	private String lessonTypeStr;
	
	private static  SchoolManager schoolManager;
	
	private static HashMap<String , String> schoolLessonTypeMap;

	public void setLessonTypeStr(String lessonTypeStr) {
		this.lessonTypeStr = lessonTypeStr;
	}
	
	public SchoolLessonTypeTag(){
		
	}
	
	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {
		Boolean schoolHasNew=(Boolean) pageContext.getServletContext().getAttribute("schoolHasNew");
		if(schoolManager == null ){
			// ��ϵͳ�л�ȡspring����
			ApplicationContext ctx = (XmlWebApplicationContext) pageContext.getServletContext().getAttribute("org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");
			schoolManager = (SchoolManager) ctx.getBean("schoolManager");
			schoolLessonTypeMap = schoolManager.getSchoolLessonType();
		}else{
			if(schoolHasNew!= null){
				ApplicationContext ctx = (XmlWebApplicationContext) pageContext.getServletContext().getAttribute("org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");
				schoolManager = (SchoolManager) ctx.getBean("schoolManager");
				schoolLessonTypeMap = schoolManager.getSchoolLessonType();
				pageContext.getServletContext().removeAttribute("lessonTypeHasNew");
			}
		}
		String lessonTypeNameStr = schoolLessonTypeMap.get(lessonTypeStr);
		try {
			if (lessonTypeNameStr == null) {
				pageContext.getOut().print("-1");
			} else {
				pageContext.getOut().print(lessonTypeNameStr);
			}
		} catch (IOException e) {
			throw new JspException("Print Error:" + e.getMessage());
		}
		return super.doStartTag();
	}
	
}
